/**
 * customer 工单模块
 * @author 710335997@qq.com
 * @time 2016年3月21日20:02:49
 */
var _ljs=require("../mmlog").root("[sever/db/ticket.js]");
var db = require("./db");
var mail=require("../channel/mail");

module.exports = {
	
	selectTicketByJobId:function(jobId,callback){
		var sql="SELECT * FROM T_TICKET";
		if(jobId.toUpperCase()!="ALL"){
			sql+=" WHERE JOBID='"+jobId+"' OR JOBID='ALL'";
		}
		db.query({
			sql: sql,
			values: null
		}, callback)
	},
	
	selectTicketSort:function(jobId,s,o,callback){
		var sql="SELECT * FROM T_TICKET";
		if(jobId.toUpperCase()!="ALL"){
			sql+=" WHERE JOBID='"+jobId+"' OR JOBID='ALL' ";
		}
		sql+=" ORDER BY "+s+" "+o;
		db.query({
			sql: sql,
			values: null
		}, callback)
	},
	
	/**
	 * 查询工单
	 * @param {Object} uuid
	 * @param {Object} callback
	 */
	selectTicketByUUID:function(uuid,callback){
		var sql="SELECT * FROM T_TICKET WHERE UUID=?";
		db.query({
			sql: sql,
			values: [uuid]
		}, callback)
	},
	
	insertTicket:function(param,callback){
		var sql="INSERT INTO `mmlink`.`t_ticket` ( `UUID`, `TICKET_ID`, `AUTHOR`, `CUSTOMER`, `JOBID`, `LEVEL`, `BUSINESS`, `LABEL`, `SUBJECT`, `CONTENT`, `FILES`, `CREATE_TIME`, `STATUS` ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0 )";
		db.query({
			sql: sql,
			values: param
		}, callback)
	},
	
	/**
	 * 删除工单
	 * @param {Object} uuid
	 * @param {Object} callback
	 */
	deleteTicket:function(uuid,callback){
		var sql="DELETE FROM `mmlink`.`t_ticket` WHERE UUID=?";
		db.query({
			sql: sql,
			values: [uuid]
		}, callback)
	},
	
	/**
	 * 更新工单状态
	 * @param {Object} status
	 * @param {Object} uuid
	 * @param {Object} callback
	 */
	updateStatus:function(status,uuid,callback){
		var sql="UPDATE T_TICKET SET STATUS=? , UPDATE_TIME=? WHERE UUID=?";
		db.query({
			sql: sql,
			values: [status,new Date().getTime(),uuid]
		}, callback)
	},
	
	/**
	 * 工单邮件回复
	 * @param {Object} data
	 * @param {Object} callback
	 */
	replyMail:function(data,callback){
		mail.send(data,callback);
	}
	
}